package com.hyzh.hj212.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.hyzh.hj212.config.ConfigManager;
import com.hyzh.hj212.enterprise.CommandTypeEnum;
import com.hyzh.hj212.enterprise.SysCodeTypeEnum;
import com.hyzh.hj212.dmo.entity.CommandEntity;
import com.hyzh.hj212.dmo.entity.SysCodeEntity;
import com.hyzh.hj212.dmo.entity.record.AirRecordEntity;
import com.hyzh.hj212.dmo.entity.record.WaterRecordEntity;
import com.hyzh.hj212.netty.HJ212MsgUtils;
import com.hyzh.hj212.repository.AirRecordRepository;
import com.hyzh.hj212.repository.WaterRecordRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;


/**
 * @author songkui
 * @since 2024/3/20 17:13
 */
@Service
@Slf4j
@AllArgsConstructor
public class LatteService {

    private final AirRecordRepository airRecordRepository;
    private final WaterRecordRepository waterRecordRepository;

    public void saveRecord(Object msg){
        Map<String, Object> map = HJ212MsgUtils.msgToGroupMap((String)msg);
        if (CollectionUtil.isEmpty(map)){
            log.error("msg to map error: msg: {}", msg);
            return;
        }

        Integer st = Optional.ofNullable(map.get("ST")).map(s -> NumberUtil.parseInt(String.valueOf(s), -1)).orElse(-1);
        Integer cn = Optional.ofNullable(map.get("CN")).map(s -> NumberUtil.parseInt(String.valueOf(s), -1)).orElse(-1);
        if (st < 1 || cn < 1 ){
            log.error("msg to map error: ST or CN invalid;  msg: {}", msg);
            return;
        }

        SysCodeEntity sysCodeEntity = ConfigManager.getInstance().getSysCodeMap().get(st);
        CommandEntity commandEntity = ConfigManager.getInstance().getCommandMap().get(cn);
        if (null == sysCodeEntity || null == commandEntity){
            log.info("msg to map: SysCodeEntity is null or CommandEntity is null;  msg: {}", msg);
            return;
        }

        if (!CommandTypeEnum.DATA.code().equals(commandEntity.getCmType())){
            log.info("msg to map: Command is not data cmd;  msg: {}", msg);
            return;
        }

        SysCodeTypeEnum sysCodeTypeEnum = SysCodeTypeEnum.ofCode(sysCodeEntity.getType());
        if (null == sysCodeTypeEnum){
            log.info("msg to map: SysCodeTypeEnum is null; [ type:{} ] msg: {}", sysCodeEntity.getType(), msg);
            return;
        }

        switch (sysCodeTypeEnum){
            case AIR:
                doAir(map);
                break;
            case WATER:
                doWater(map);
                break;
            case SOIL:
                doSoil(map);
                break;
            default:
                doOther(map);
        }

    }


    private void doAir(Map<String, Object> map){

        AirRecordEntity airRecordEntity = new AirRecordEntity();
        airRecordEntity.setCn(NumberUtil.parseInt(String.valueOf(map.get("CN")), -1));
        airRecordEntity.setQn(String.valueOf(map.get("QN")));
        airRecordEntity.setSt(NumberUtil.parseInt(String.valueOf(map.get("ST")), -1));
        airRecordEntity.setMn(String.valueOf(map.get("MN")));
        JSONObject cpJson = (JSONObject) map.get("CP");
        airRecordEntity.setCp(cpJson.toJSONString());
        airRecordEntity.setAddTime(LocalDateTime.now());

        airRecordRepository.save(airRecordEntity);

    }

    private void doWater(Map<String, Object> map){
        WaterRecordEntity entity = new WaterRecordEntity();
        entity.setCn(NumberUtil.parseInt(String.valueOf(map.get("CN")), -1));
        entity.setQn(String.valueOf(map.get("QN")));
        entity.setSt(NumberUtil.parseInt(String.valueOf(map.get("ST")), -1));
        entity.setMn(String.valueOf(map.get("MN")));
        JSONObject cpJson = (JSONObject) map.get("CP");
        entity.setCp(cpJson.toJSONString());
        entity.setAddTime(LocalDateTime.now());
        waterRecordRepository.save(entity);

    }


    private void doSoil(Map<String, Object> map) {
    }
    private void doOther(Map<String, Object> map) {
    }



}
